// pages/user/myorder.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hiddenLoading: false,
        userInfo: {},
        order_type: -1,
        orderList: [],
        thh_index: null,
        thh_hiddenmodalput: true,
        reasonInp: null,
        mobileInp: null,
        show: false,
        steps: [{
                text: '步骤一',
                desc: '描述信息',
                // inactiveIcon: 'location-o',
                // activeIcon: 'success',
            },
            {
                text: '步骤二',
                desc: '描述信息',
                // inactiveIcon: 'like-o',
                // activeIcon: 'plus',
            },
            {
                text: '步骤三',
                desc: '描述信息',
                // inactiveIcon: 'star-o',
                // activeIcon: 'cross',
            },
            {
                text: '步骤四',
                desc: '描述信息',
                // inactiveIcon: 'phone-o',
                // activeIcon: 'fail',
            },
        ],

        showWuliu: false,
        wuliuItem: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        console.log(options.type);
        if (typeof (options.type) != "undefined") {
            that.setData({
                order_type: options.type
            });
        }
    },

    //下拉刷新
    onPullDownRefresh: function () {
        this.getOrder();
        wx.stopPullDownRefresh();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (opt) {
        var that = this
        let meIsShow = wx.getStorageSync('meIsShow')
        //用户授权登录
        app.login(function (userInfo) {
            that.setData({
                userInfo: userInfo,
                meIsShow: meIsShow
            });
            that.getOrder();
        });
    },

    //切换订单
    changeOrder: function (e) {
        var that = this;
        var otype = e.currentTarget.dataset.type
        that.setData({
            order_type: otype
        });
        that.getOrder();
    },

    //获取订单列表
    getOrder: function () {
        var that = this;
        var paraArr = new Array();
        paraArr['state'] = that.data.order_type;
        paraArr['user_id'] = that.data.userInfo.userID
        // 164;
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api_order',
            data: {
                state: paraArr['state'],
                user_id: paraArr['user_id'],
                sign: sign
            },
            method: 'GET',
            header: {},
            success: function (res) {
                if (res?.data?.length > 0) {
                    res.data.forEach(item => {
                        let list = []
                        item._express_sn = []
                        if (item.express_sn != '') {
                            list = item.express_sn.split(',')
                            list.forEach(o => {
                                item._express_sn.push({
                                    name: o,
                                    subname: item.express_title
                                })
                            })
                        }
                    })
                }

                that.setData({
                    orderList: res.data,
                    hiddenLoading: true
                });
            }
        })
    },
    goCardRecord: function () {
        console.log(111)
        app.redirect('cardRecord/cardRecord')
    },
    //去付款
    goPay: function (e) {
        app.redirect('pay/index', 'sn=' + e.currentTarget.dataset.sn);
    },

    //去评价
    goComment: function (e) {
        app.redirect('user/mycomment', 'id=' + e.currentTarget.dataset.id);
    },

    //取消订单
    cancelOrder: function (e) {
        var that = this;
        var m_id = e.currentTarget.dataset.id
        if (!m_id) return;
        var paraArr = new Array();
        paraArr['id'] = m_id;
        paraArr['m_type'] = 'cancel';
        paraArr['user_id'] = that.data.userInfo.userID;
        var sign = app.signature(paraArr);
        wx.showModal({
            title: '提示',
            content: '确认要取消吗？',
            success: function (res) {
                if (res.confirm) {
                    wx.request({
                        url: rootDocment + '/api_order/' + m_id,
                        data: {
                            m_type: 'cancel',
                            user_id: paraArr['user_id'],
                            sign: sign
                        },
                        method: 'PUT',
                        header: {},
                        success: function (res) {
                            app.redirect('user/myorder', 'type=' + that.data.order_type);
                        }
                    })
                }
            }
        })
    },

    //确认收货
    shOrder: function (e) {
        var that = this;
        var m_id = e.currentTarget.dataset.id
        if (!m_id) return;
        var paraArr = new Array();
        paraArr['id'] = m_id;
        paraArr['m_type'] = 'received';
        paraArr['user_id'] = that.data.userInfo.userID;
        var sign = app.signature(paraArr);
        wx.showModal({
            title: '提示',
            content: '确认要收货吗？',
            success: function (res) {
                if (res.confirm) {
                    wx.request({
                        url: rootDocment + '/api_order/' + m_id,
                        data: {
                            m_type: paraArr['m_type'],
                            user_id: paraArr['user_id'],
                            sign: sign
                        },
                        method: 'PUT',
                        header: {},
                        success: function (res) {
                            app.redirect('user/myorder', 'type=3');
                        }
                    })
                }
            }
        })
    },

    //查看物流
    showSend: function (e) {
        var that = this;
        var m_id = e.currentTarget.dataset.id
        if (!m_id) return;
        var paraArr = new Array();
        paraArr['order_id'] = m_id;
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api/comm_get/getSend',
            data: {
                order_id: paraArr['order_id'],
                sign: sign
            },
            method: 'GET',
            header: {},
            success: function (res) {
                wx.showModal({
                    title: res.data.sendType,
                    content: res.data.sendNo,
                    showCancel: false,
                    success: function (res) {}
                })
            }
        })
    },
    //查看物流
    showSends: function (e) {
        let item = e.currentTarget.dataset.item

        if (item.kind) {
            wx.navigateTo({
                url: `/pages/orderTraceability/traceabilitydetail?id=${item.order_sn}&goodsType=${item.kind}`,
            })
        } else {
            this.setData({
                wuliuItem: item,
                showWuliu: true
            })
        }
    },

    // 物流选择关闭
    onCloseActionSheet() {
        this.setData({
            showWuliu: false
        })
    },

    // 物流选择
    onActionSheet(e) {
        let detail = e.detail
        let item = this.data.wuliuItem

        this.onCloseActionSheet()

        if (item.kind) {
            wx.navigateTo({
                url: `/pages/orderTraceability/traceabilitydetail?id=${item.order_sn}&goodsType=${item.kind}`,
            })
        } else {
            const now = new Date();
            const targetDate = new Date(item.push_time);
            const threeMonthsAgo = new Date(now.setMonth(now.getMonth() - 3));



            if (targetDate < threeMonthsAgo) {
                wx.navigateTo({
                    url: `/subpage/logistics_info/logistics_info?id=${item.express_sn}`,
                })

                return
            }

            wx.navigateTo({
                url: `plugin://kdzs-plugin/logistics-list?num=${detail.name}&phone=${item.tel.substring(7)}`,
            })
        }


    },


    // 关闭物流弹窗
    onClose() {
        this.setData({
            show: false
        });
    },

    //删除订单
    delOrder: function (e) {
        var that = this;
        var m_id = e.currentTarget.dataset.id
        if (!m_id) return;
        var paraArr = new Array();
        paraArr['id'] = m_id;
        paraArr['user_id'] = that.data.userInfo.userID;
        var sign = app.signature(paraArr);
        wx.showModal({
            title: '提示',
            content: '确认要删除吗？',
            success: function (res) {
                if (res.confirm) {
                    wx.request({
                        url: rootDocment + '/api_order/' + m_id,
                        data: {
                            user_id: paraArr['user_id'],
                            sign: sign
                        },
                        method: 'DELETE',
                        header: {},
                        success: function (res) {
                            app.redirect('user/myorder', 'type=' + that.data.order_type);
                        }
                    })
                }
            }
        })
    },
    // 控制退货弹窗
    meExchanges(e) {
        this.setData({
            thh_index: e.currentTarget.dataset.id,
            thh_hiddenmodalput: false
        })


    },
    // 关闭退货弹窗
    thh_cancel() {
        console.log(2222);
        this.setData({
            thh_hiddenmodalput: true
        })
    },
    // 确定退货
    thh_confirm() {
        var that = this;
        console.log(that.data.reasonInp, that.data.mobileInp);
        if (that.data.reasonInp == null) {
            wx.showToast({
                title: '请输入退换货原因',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (that.data.mobileInp == null) {
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 2000
            })
            return
        }
        var paraArr = new Array();
        paraArr['user_id'] = that.data.userInfo.userID;
        paraArr['order_id'] = that.data.thh_index;
        paraArr['reason'] = that.data.reasonInp;
        paraArr['mobile'] = that.data.mobileInp;
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api/user/refund',
            data: {
                user_id: paraArr['user_id'],
                order_id: paraArr['order_id'],
                reason: paraArr['reason'],
                mobile: paraArr['mobile'],
                // sign: sign
            },
            method: 'POST',
            header: {},
            success: function (res) {
                console.log(res.data, '11111');
                if (res.data.code == 1001) {
                    wx.showToast({
                        title: '申请成功',
                        icon: 'none',
                        duration: 2000
                    })
                    wx.navigateTo({
                        url: '/pages/user/myorder?type=4',
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 2000
                    })
                }
                that.setData({
                    reasonInp: null,
                    mobileInp: null,
                    thh_hiddenmodalput: true
                })
            }
        })

    },
    //退货原因
    on_reason(e) {
        console.log(e.detail.value);
        this.setData({
            reasonInp: e.detail.value
        })
    },
    //退货手机号
    on_mobile(e) {
        this.setData({
            mobileInp: e.detail.value
        })
    },
})